package 力扣;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author yyq
 * @create 2022-04-07 20:58
 */
public class leetcode111 {
    public int minDepth(TreeNode root) {
        int mini = 1;
        int LayerNodes = 1;
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null) return mini;
        queue.add(root);

        while (!queue.isEmpty()){
            int temp = 0;
            while (LayerNodes!=0){
                TreeNode treeNode = queue.poll();
                if(treeNode.left==null&&treeNode.right==null) return mini;
                if(treeNode.left!=null)
                {
                    queue.add(treeNode.left);
                    temp++;
                }
                if(treeNode.right!=null)
                {
                    queue.add(treeNode.right);
                    temp++;
                }
                LayerNodes--;

            }
            LayerNodes=temp;
            mini++;
        }
        return mini;
    }



    private static int preOrder(int temp, int[] mini, TreeNode root) throws Exception {
        temp++;
        if (temp>mini[0]) mini[0]=temp;
        if(root.left==null&&root.right==null) {
            throw new Exception();

        }
        if(root.left!=null) {
            temp = preOrder(temp,mini,root.left);
            temp--;
        }
        if(root.right!=null){
            temp = preOrder(temp,mini,root.right);
            temp--;
        }
        return temp;

    }
}
